#!/bin/csh

# max2prot - create maxplus2 license file

set FILE=license.altera
set default_dir=/usr/maxplus2
set dir=$1

clear
echo ""
echo "MAX+PLUS II License File Installation"
echo "Copyright (c) Altera Corporation 1995"

get_install_directory:
if ($dir == "") then
	echo ""
	echo "Type the full pathname of the system directory where MAX+PLUS II"
	echo "has been installed (default: $default_dir): "
	set dir=$<

	if ($dir == "") then
		set dir=$default_dir
	endif
endif

if (! -d $dir) then
	echo ""
	echo "$dir is not a valid directory name"	
	set dir=""
	goto get_install_directory
endif

if (! -d $dir/adm ) then
	echo ""
	echo "$dir/adm does not exist, MAX+PLUS II may not have"
	echo  "been installed properly in $dir."
	echo ""
	set dir=""
	goto get_install_directory
endif

if (-f $dir/adm/$FILE) then 
	echo    ""
	echo    "Choose one of the following:"
	echo    ""
	echo    "  1. Create a new license"
	echo    "  2. Update an existing license"
	get_create_choice:
	echo    ""
	echo -n "(choose 1 or 2 only): "
	
	set choice=$<

	if ($choice != "1" && $choice != "2") then
		goto get_create_choice
	endif

	if ($choice == "1") then
		mv -f $dir/adm/$FILE $dir/adm/$FILE.sav
	endif
else
	set choice=1
endif


if ($choice == 1) then
	echo    ""
	echo "The following information is required to install the network"
	echo "licensing file:"
	echo ""
	echo "  1. The number of license servers at your site."
	echo "  2. The host name and the host ID for each license server."
	echo "  3. The MAX+PLUS II feature being licensed."
	echo "  4. The maximum number of users that MAX+PLUS II will"
    echo "     support concurrently."
	echo "  5. The license expiration date."
	echo "  6. The authorization code from Altera Customer Service."
	echo ""

else 
	echo    ""
	echo "The following information is required to update an existing"
    echo "license file:"
	echo ""
	echo "  1. The MAX+PLUS II feature being licensed."
	echo "  2. The maximum number of users that MAX+PLUS II will"
    echo "     support concurrently."
	echo "  3. The license expiration date."
	echo "  4. The authorization code from Altera Customer Service."
	echo ""
endif

read_ok_to_cont:
echo -n "Do you want to continue? (y/n) "
set cont=$<

if ($cont == "n" || $cont == "N") then
	exit 0
else if ($cont != "y" && $cont != "Y") then
	goto read_ok_to_cont
endif

begin_get_info:
if ($choice == "1") then
	get_n_servers:
	echo ""
	echo -n "Enter the number of license servers: (1/3) "
	set n_servers=$<

	if ($n_servers != 1 && $n_servers != 3) then
		echo ""
		echo "Illegal number of servers: $n_servers."
		echo "Only 1 or 3 are legal."
		goto get_n_servers
	endif
	
	set host1=""
	set host2=""
	set host3=""
	set hostid1=""
	set hostid2=""
	set hostid3=""

	if ($n_servers == 1) then
		echo ""
		get_one_server:
		echo -n "Enter the hostname for the license server: "
		set host1=$<
		if ($host1 == "") then
			goto get_one_server
		endif
		get_one_server_id:
		echo -n "Enter the hostid for "$host1": "
		set hostid1=$<
		if ($hostid1 == "") then
			goto get_one_server_id
		endif
	else
		echo ""
		get_host1:
		echo -n "Enter the hostname for license server #1: "
		set host1=$<
		if ($host1 == "") then
			goto get_host1
		endif
		get_hostid1:
		echo -n "Enter the hostid for "$host1": "
		set hostid1=$<
		if ($hostid1 == "") then
			goto get_hostid1
		endif

		echo ""
		get_host2:
		echo -n "Enter the hostname for license server #2: "
		set host2=$<
		if ($host2 == "") then
			goto get_host1
		endif
		get_hostid2:
		echo -n "Enter the hostid for "$host2": "
		set hostid2=$<
		if ($hostid2 == "") then
			goto get_hostid2
		endif

		echo ""
		get_host3:
		echo -n "Enter the hostname for license server #3: "
		set host3=$<
		if ($host3 == "") then
			goto get_host3
		endif
		get_hostid3:
		echo -n "Enter the hostid for "$host3": "
		set hostid3=$<
		if ($hostid3 == "") then
			goto get_hostid3
		endif		
	endif
endif

set default_exp_date="01-jan-2000"
set default_n_users=1

get_feature_info:
echo    ""
echo    "Which feature is being installed? Choose one:"
echo    ""
echo    "  1. maxplus2      (MAX+PLUS II Base System)"
echo    "  2. maxplus2tan   (Timing Analyzer)" 
echo    "  3. maxplus2vhdl  (VHDL Synthesis)"
echo    "  4. maxplus2ext   (...)"
echo    ""
echo    "  Q. Quit"
get_feat:
echo    ""
echo -n "(choose 1, 2, 3, 4, or Q): "
set feat=$<

set feature=""

switch ($feat)
case 1:
	set feature="maxplus2"
	breaksw
case 2:
	set feature="maxplus2tan"
	breaksw
case 3:
	set feature="maxplus2vhdl"
	breaksw
case 4:
	set feature="maxplus2ext"
	breaksw
case Q:
case q:
	exit 0
	breaksw
default:
	goto get_feat
	breaksw
endsw

echo ""
echo -n "Enter the software expiration date [$default_exp_date]: "
set exp_date=$<
if ($exp_date == "") then
	set exp_date=$default_exp_date	
else
	set default_exp_date=$exp_date
endif

echo ""
echo -n "Enter the maximum number of users [$default_n_users]: "
set n_users=$<
if ($n_users == "") then
	set n_users=$default_n_users
else
	set default_n_users=$n_users
endif

get_code:
echo ""
echo -n "Enter the authorization code: "
set code=$<
if ($code == "") then 
	goto get_code
endif

get_confirm:
echo ""
echo -n "Is the information correct? (y/n/q) "
set confirm=$<

switch ($confirm)
case y:
case Y:
	breaksw
case n:
case N:
	goto get_feature_info
	breaksw
case q:
case Q:
	exit 0
	breaksw
default:
	goto get_confirm
	breaksw
endsw


if ($choice == 1) then
	echo "SERVER $host1 $hostid1 1800" >> $dir/adm/$FILE

	if ($n_servers == 3) then
		echo "SERVER $host2 $hostid2 1800" >> $dir/adm/$FILE
		echo "SERVER $host3 $hostid3 1800" >> $dir/adm/$FILE
	endif
	echo "DAEMON alterad $dir/adm/alterad " >> $dir/adm/$FILE
	set choice=2
endif

echo FEATURE $feature alterad 0.0 $exp_date $n_users $code \"ALTERA\" >> $dir/adm/$FILE

goto get_feature_info
